package cn.wh.basic.controller;

import cn.wh.basic.dto.LoginDto;
import cn.wh.basic.dto.WeChatDto;
import cn.wh.basic.exception.BusinessException;
import cn.wh.basic.service.ILoginService;
import cn.wh.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService iLoginService;

    /**
     * 账号密码登录
     * @param dto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> token = iLoginService.account(dto);
            return AjaxResult.my().setResultObj(token);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }

    /**
     * 微信三方扫码
     * @param dto
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WeChatDto dto){
        try {
            return iLoginService.wechat(dto);
        } catch (BusinessException be) {//我们自己的业务异常，我们需要自己处理
            be.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }

    /**
     * 微信绑定账号
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WeChatDto dto){
        try {
            Map<String, Object> map = iLoginService.binder(dto);
            return AjaxResult.my().setResultObj(map);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }



}
